package com.esp.soccertutor.uc;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;


import com.esp.soccertutor.R;
import com.esp.soccertutor.util.Pref;
import com.esp.soccertutor.zlibrary.core.options.ZLColorOption;
import com.esp.soccertutor.zlibrary.core.resources.ZLResource;
import com.esp.soccertutor.zlibrary.core.util.ZLColor;
import com.esp.soccertutor.zlibrary.ui.android.util.ZLAndroidColorUtil;

public class CustomDialog {

	private Context mContext = null;
	private LayoutInflater mLayoutInflater = null;
	private View mView;
	private boolean mIsShowing = false;
	private PopupWindow mPopupWindow = null;
	public boolean isShowing() { return mIsShowing; }
	
	private SeekBar myRedSlider,myGreenSlider,myBlueSlider;
	private final GradientDrawable myPreviewDrawable = new GradientDrawable();
	private ZLColorOption myOption;
	private Button btnPositive,btnNagative;
	private TextView txtTitle;
	
	private String KEY_NAME;
	
	public CustomDialog(Context context,LayoutInflater lo,ZLColorOption option,String title,TextView tview, String KEY_NAME){
		mContext=context;
		mLayoutInflater=lo;
		mView= mLayoutInflater.inflate(R.layout.color_dialog, null);
		myOption=option;
		this.KEY_NAME = KEY_NAME;
		
		btnPositive=(Button)mView.findViewById(R.id.btnPositive);
		btnNagative=(Button)mView.findViewById(R.id.btnNagative);
		txtTitle=(TextView)mView.findViewById(R.id._tvColorTitle);
		txtTitle.setText(title);
		
		final ZLColor color = myOption.getValue();
		myRedSlider = createSlider(mView, R.id.color_red, color.Red, "red");
		myGreenSlider = createSlider(mView, R.id.color_green, color.Green, "green");
		myBlueSlider = createSlider(mView, R.id.color_blue, color.Blue, "blue");

		final View colorBox = mView.findViewById(R.id.color_box);
		colorBox.setBackgroundDrawable(myPreviewDrawable);
		myPreviewDrawable.setCornerRadius(7);
		myPreviewDrawable.setColor(ZLAndroidColorUtil.rgb(color));

		final SeekBar.OnSeekBarChangeListener listener = new SeekBar.OnSeekBarChangeListener() {
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				myPreviewDrawable.setColor(Color.rgb(
					myRedSlider.getProgress(),
					myGreenSlider.getProgress(),
					myBlueSlider.getProgress()
				));
				myPreviewDrawable.invalidateSelf();
			}

			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			public void onStopTrackingTouch(SeekBar seekBar) {
				myPreviewDrawable.setColor(Color.rgb(
					myRedSlider.getProgress(),
					myGreenSlider.getProgress(),
					myBlueSlider.getProgress()
				));
				myPreviewDrawable.invalidateSelf();
			}
		};
		myRedSlider.setOnSeekBarChangeListener(listener);
		myGreenSlider.setOnSeekBarChangeListener(listener);
		myBlueSlider.setOnSeekBarChangeListener(listener);
		
		setOnClickEvent(btnPositive,"OK",tview);
		setOnClickEvent(btnNagative,"Cancel",tview);
		
	}
	
	private SeekBar createSlider(View view, int id, int value, String resourceKey) {
		final SeekBar slider = (SeekBar)view.findViewById(id);
		slider.setProgress(value);
		slider.setProgressDrawable(new SeekBarDrawable(
			slider.getProgressDrawable(),
			ZLResource.resource("color").getResource(resourceKey).getValue(),
			slider
		));
		return slider;
	}
	private void setOnClickEvent(Button eventButton,final String resourceKey,final TextView tview){
		
		eventButton.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				
				if(resourceKey.equals("OK")){
					
					ZLColor ObjColor = new ZLColor(myRedSlider.getProgress(), myGreenSlider.getProgress(), myBlueSlider.getProgress());
					
					myOption.setValue(ObjColor);
					
					tview.setBackgroundColor(ZLAndroidColorUtil.rgb(
						new ZLColor(myRedSlider.getProgress(),myGreenSlider.getProgress(),myBlueSlider.getProgress())
						));
					
					Pref.setValue(KEY_NAME, String.valueOf(ObjColor.getIntValue()));
							
					hide();
				}else{
					
					hide();
				}
				
				
			}
		});
	}
	
	
	static class SeekBarDrawable extends Drawable {
		private final SeekBar mySlider;
		private final Drawable myBase;
		private final String myText;
		private final Paint myPaint;
		private final Paint myOutlinePaint;
		private boolean myLabelOnRight;

		public SeekBarDrawable(Drawable base, String text, SeekBar slider) {
			mySlider = slider;
			myBase = base;
			myText = text;

			myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
			myPaint.setTypeface(Typeface.DEFAULT_BOLD);
			myPaint.setColor(Color.BLACK);
			myPaint.setAlpha(255);

			myOutlinePaint = new Paint(myPaint);
			myOutlinePaint.setStyle(Paint.Style.STROKE);
			myOutlinePaint.setStrokeWidth(3);
			myOutlinePaint.setColor(0xFFAAAAAA);

			myLabelOnRight = mySlider.getProgress() < 128;
		}

		@Override
		protected void onBoundsChange(Rect bounds) {
			myBase.setBounds(bounds);
		}
		
		@Override
		protected boolean onStateChange(int[] state) {
			invalidateSelf();
			return false;
		}
		
		@Override
		public boolean isStateful() {
			return true;
		}
		
		@Override
		protected boolean onLevelChange(int level) {
			if (level < 4000) {
				myLabelOnRight = true;
			} else if (level > 6000) {
				myLabelOnRight = false;
			}
			return myBase.setLevel(level);
		}
		
		@Override
		public void draw(Canvas canvas) {
			myBase.draw(canvas);

			final Rect bounds = getBounds();
			final int textSize = bounds.height() * 2 / 3;
			myPaint.setTextSize(textSize);
			myOutlinePaint.setTextSize(textSize);
			final Rect textBounds = new Rect();
			myPaint.getTextBounds("a", 0, 1, textBounds);
			final String text = myText + ": " + mySlider.getProgress();
			final float textWidth = myOutlinePaint.measureText(text);
			final float x = myLabelOnRight ? bounds.width() - textWidth - 6 : 6;
			final float y = bounds.height() / 2 + textBounds.height();
			canvas.drawText(text, x, y, myOutlinePaint);
			canvas.drawText(text, x, y, myPaint);
		}

		@Override
		public int getOpacity() {
			return PixelFormat.TRANSLUCENT;
		}

		@Override
		public void setAlpha(int alpha) {
		}

		@Override
		public void setColorFilter(ColorFilter cf) {
		}
	}

	public synchronized void show(View v) {
		mIsShowing = true;
		if (mPopupWindow != null) return; //already showing
		Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
	
		mPopupWindow = new PopupWindow(mView,android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, false);
        mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        mPopupWindow.setWidth(display.getWidth());
        mPopupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
	}
	public synchronized void hide() {
		mIsShowing = false;
		if (mPopupWindow != null) {
			mPopupWindow.dismiss();
			mPopupWindow = null;
		}
		return;
	}
	
}
